<?php
if (!isset($model))
    $model = new BDynamicFormModel ();
$priceConfigNameField = 'media_priceConfigName_priceConfig_info';
$priceChangeWayField = 'media_priceChangeWay_priceConfig_info';
$priceChangeAmountField = 'media_priceChangeAmount_priceConfig_info';
$priceChangeUnitField = 'media_priceChangeUnit_priceConfig_info';
$model->addField($priceConfigNameField);
$model->addField($priceChangeWayField);
$model->addField($priceChangeAmountField);
$model->addField($priceChangeUnitField);

$attributeNameField = 'media_name_priceConfig_attribute';
$attributeAliasField = 'media_alias_priceConfig_attribute';
$attributeTypeField = 'media_type_priceConfig_attribute';
$model->addField($attributeNameField);
$model->addField($attributeAliasField);
$model->addField($attributeTypeField);

$priceConfigAttributeView = array();

if (isset($orderPriceConfigId) && $orderPriceConfigId > 0) {
    $priceConfigTable = new BPSheet('priceConfig');
    $priceConfig = $priceConfigTable->getListById($orderPriceConfigId);

    $priceConfigAttributeTable = new BPSheet('priceConfigAttribute', $priceConfig);
    $priceConfigAttributeView = $priceConfigAttributeTable->getView();

    /**
     * below code will populate attribute for product
     */
    foreach ($priceConfigAttributeView as $id => $priceConfigAttribute) {
        $nameItem = $priceConfigAttribute->getItem('name');
        $typeItem = $priceConfigAttribute->getItem('type');
        if ($nameItem instanceof BTreeItem) {
            $name = $nameItem->getValue();
            $attributeField = 'media_' . $name . '_priceConfig_info';
            $model->addField($attributeField);
            //get value of attribute in product 
            $attributeItem = $priceConfig->getItem($name);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
                $model->$attributeField = $attributeValue;
            }
        }
        if ($typeItem instanceof BTreeItem) {
            $type = $nameItem->getValue();
            $attributeField = 'media_' . $type . '_priceConfig_info';
            $model->addField($attributeField);
        }
    }

    $priceConfigNameItem = $priceConfig->getItem('priceConfigName');
    if ($priceConfigNameItem instanceof BTreeItem) {
        $priceConfigName = $priceConfigNameItem->getValue();
        $model->$priceConfigNameField = $priceConfigName;
    }
    $changeWayItem = $priceConfig->getItem('priceChangeWay');
    if ($changeWayItem instanceof BTreeItem) {
        $priceChangeWay = $changeWayItem->getValue();
        $model->$priceChangeWayField = $priceChangeWay;
    }
    $changeAmountItem = $priceConfig->getItem('priceChangeAmount');
    if ($changeAmountItem instanceof BTreeItem) {
        $priceChangeAmount = $changeAmountItem->getValue();
        $model->$priceChangeAmountField = $priceChangeAmount;
    }
    $changeUnitItem = $priceConfig->getItem('priceChangeUnit');
    if ($changeUnitItem instanceof BTreeItem) {
        $priceChangeUnit = $changeUnitItem->getValue();
        $model->$priceChangeUnitField = $priceChangeUnit;
    }
}
?>
<?php
$app = Blyn::app()->getActiveApp();
?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "addPriceConfigForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
$viewData = array();
if (isset($eventId))
    $viewData['eventId'] = $eventId;
if (isset($configId))
    $viewData['configId'] = $configId;
if (isset($orderPriceConfigId))
    $viewData['orderPriceConfigId'] = $orderPriceConfigId;
$url = $control->getAjaxLinkUrl('saveOrderPriceConfig', NULL, $viewData);
$updateId = $control->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function savePriceConfig() {

        var formData = new FormData($("#addPriceConfigForm")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            //datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>


<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
            <th><?php echo '操作'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <?php echo '配置命名'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $priceConfigNameField, array('placeholder' => 'priceConfig name' . '...', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td></td>
        </tr>
        <tr>
            <td>
                <?php echo '调整价格方式'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeRadioButtonList($model, $priceChangeWayField, array('addPrice' => '加价',
                    'discount' => '降价'), array(
                    'labelOptions' => array('style' => 'display:inline'), // add this code
                    'separator' => '  ',
                ));
                //echo $form->activeRadio($model, $changePriceTypeField, array('placeholder' => '调整价格方式' . '...', 'required' => true));
                ?>
            </td>
        </tr>
        <tr><td> <?php echo '调整量'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $priceChangeAmountField, array('placeholder' => 'change amount' . '...', 'required' => true));
                ?>
            </td>
            <td></td>
        </tr> 
        <tr><td> <?php echo '调整单位'; ?> </td>
            <td>
                <?php
                echo CHtml::activeDropDownList($model, $priceChangeUnitField, $app->getPriceChangeUnitList(), array('placeholder' => 'change unit' . '...', 'required' => true));
                ?>
            </td>
            <td></td>
        </tr>

        <?php
        if (isset($orderPriceConfigId) && $orderPriceConfigId > 0) {
            foreach ($priceConfigAttributeView as $priceConfigAttribute) {
                $attributeId = $priceConfigAttribute->getId();
                ?>
                <tr>
                    <?php
                    $nameItem = $priceConfigAttribute->getItem('name');
                    $typeItem = $priceConfigAttribute->getItem('type');
                    $aliasItem = $priceConfigAttribute->getItem('alias');
                    $name = '';
                    $type = '';
                    $alias = '';
                    $attributeValue = '';
                    if ($nameItem instanceof BTreeItem) {
                        $name = $nameItem->getValue();
                        $attributeField = 'media_' . $name . '_priceConfig_info';
                        $attributeItem = $priceConfig->getItem($name);
                        if ($attributeItem instanceof BTreeItem) {
                            $attributeValue = $attributeItem->getValue();
                            $model->$attributeField = $attributeValue;
                        }
                    }
                    if ($typeItem instanceof BTreeItem) {
                        $type = $typeItem->getValue();
                        //get typeData from define in attribute
                        $typeData = $app->getModelDataByDataType($typeItem);
                    }
                    if ($aliasItem instanceof BTreeItem) {
                        $alias = $aliasItem->getValue();
                    }
                    if ($alias == '')
                        $alias = $name;
                    ?>
                    <td> <?php echo $alias; ?>  </td>   
                    <td> <?php
                        $modelData = array();
                        $modelData['model'] = $model;
                        $modelData['field'] = $attributeField;
                        $modelData['data'] = $typeData;
                        if (isset($form))
                            $modelData['form'] = $form;
                        echo $app->showActiveFieldByDataType($type, $modelData);
                        ?>  
                    </td>
                    <td>
                        <?php
                        $viewData['htmlOptions'] = 'btn';
                        $viewData['attributeId'] = $attributeId;
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deletePriceConfigAttribute'), $viewData);
                        ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>
<?php
$this->endWidget();
//echo CHtml::endForm();
?>
<button class="btn" onclick="javascript:savePriceConfig()"><?php echo '保存价格配置' ?></button>